import { contextBridge, ipcRenderer } from 'electron';
import { electronAPI } from '@electron-toolkit/preload';

// Custom APIs for renderer
const api = {
  getHardwareInfo: () => ipcRenderer.invoke('get-hardware-info'),
  getHardwareInfoType: (hardwareType) => ipcRenderer.invoke('get-hardware-info-type', hardwareType),
  axiosRequest: (config) => ipcRenderer.invoke('axios-request', config),
  openWebsocket: (url) => ipcRenderer.invoke('openWebsocket', url),
  closeWebsocket: () => ipcRenderer.invoke('closeWebsocket'),
  getWebsocketStatus: () => ipcRenderer.invoke('getWebsocketStatus'),
  createWindow: (route) => ipcRenderer.invoke('createWindow', route),
  sendWebsocketMsg: (sendContent) => ipcRenderer.invoke('sendWebsocketMsg', sendContent)
};

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI);
    contextBridge.exposeInMainWorld('api', api);
  } catch (error) {
    console.error(error);
  }
} else {
  window.electron = electronAPI;
  window.api = api;
}
